const indexDb = window.indexedDB || (window as any).mozIndexedDB || (window as any).webkitIndexedDB
class IndexDbCache {
    public _db:any = null 
    public _request:any = null
    public _dbName = "list"
    public _dbVersion = 1
    public _storeName = "errorList"
    public _transaction:any = null
    // 打开数据库
    openDb(name: string) {
        this._dbName = name
        return new Promise((resolve,reject) => {
            this._request = indexDb.open(this._dbName,this._dbVersion)
            // 创建成功
            this._request.onsuccess = (e:any) => {
                this._db = e.target.result
                resolve(e)
            }
            // 创建失败
            this._request.onerror = (e: any) => {
                console.log("数据创建失败")
                console.log(e)
                reject(e)
            }
            // 数据库升级和首次创建的时候调用事件
            this._request.onupgradeneeded = (e: any) => {
                this._db = e.target.result
                if (!this._db.objectStoreNames.contains(this._storeName)) {
                    this._db.createObjectStore(this._storeName,{
                        keyPath: "id",
                        autoIncrement: true
                    })
                }
            }
        })
    }
    // 关闭数据库
    closeDb() {
        this._db.close()
    }
    addData(data:any) {
        return new Promise((resolve,reject) => {
            let transaction = this._db.transaction(this._storeName,"readwrite")
            let store = transaction.objectStore(this._storeName)
            let response = store.add(data)
            response.onsuccess = (e: any) => {
                console.log("添加成功")
                resolve(e)
            }
            response.onerror = (e: any) => {
                console.log("添加失败")
                reject(e)
            }
        })
    }
    // 查询所有的数据
    getAllData() {
        return new Promise((resolve,reject) => {
           let store = this._db.transaction(this._storeName,"readwrite").objectStore(this._storeName)
           let result = store.getAll()
           result.onsuccess = (e) => {
             resolve(e.target.result)
           }
           result.onerror = (e) => {
             reject(e.target.result)
           }
        })
    }
    // 查询当条数据
    getData(id:number) {
        return new Promise((resolve, reject) => {
            let store = this._db.transaction(this._storeName,"readwrite").objectStore(this._storeName)
            let result = store.get(id)
            result.onsuccess = (e) => {
              resolve(e)
            }
            result.onerror = (e) => {
              reject(e)
            }
        })
    }
    countNum() {
        return new Promise((resolve,reject) => {
            let store = this._db.transaction(this._storeName,"readwrite").objectStore(this._storeName)
            let result = store.count()
            result.onsuccess = (e) => {
              resolve(e.target.result)
            }
            result.onerror = (e) => {
              reject(e)
            }
        })
    }
    // 删除数据
    deleteData() {
        return new Promise((resolve,reject) => {
            let store = this._db.transaction(this._storeName,"readwrite").objectStore(this._storeName)
            let result = store.clear()
            result.onsuccess = (e) => {
              resolve(e)
            }
            result.onerror = (e) => {
              reject(e)
            }
        })
    }

    // 在IndexDbCache类中添加删除单条记录的方法
    deleteById(id: number) {
        return new Promise((resolve, reject) => {
            let store = this._db.transaction(this._storeName, "readwrite").objectStore(this._storeName)
            let result = store.delete(id)
            result.onsuccess = (e: any) => {
                resolve(e)
            }
            result.onerror = (e: any) => {
                reject(e)
            }
        })
    }



// 添加按条件查询的方法
getDataByCondition(condition: any) {
    return new Promise((resolve, reject) => {
        let store = this._db.transaction(this._storeName, "readonly").objectStore(this._storeName)
        let result = store.getAll()
        result.onsuccess = (e) => {
            const allData = e.target.result
            const filteredData = allData.filter((item: any) => {
                return Object.keys(condition).every(key => {
                    if (condition[key] === '') return true
                    return item[key] && item[key].toString().includes(condition[key])
                })
            })
            resolve(filteredData)
        }
        result.onerror = (e) => {
            reject(e)
        }
    })
}
}

export default new IndexDbCache